import ccclass = cc._decorator.ccclass;
import property = cc._decorator.property;
import {wxUtils} from "./wx/wxUtils";

declare const sharedCanvas

@ccclass
export default class GameScene extends cc.Component {
  @property(cc.Sprite)
  private subDomain: cc.Sprite = null;

  private speedX = 0;
  @property
  private skySpeed = 0;

  private player: cc.Node = null;
  private state: number = 0;
  private xinxinbg01: cc.Node = null;
  private xinxinbg02: cc.Node = null;
  private xinxinbg03: cc.Node = null;
  private score: cc.Node = null;
  private Ground: cc.Node = null;
  private texture: cc.Texture2D = null;
  private eatIndex: number = 0;

  onLoad() {
    (window as any).Game.curScene = this;
    this.player = this.node.getChildByName("player");
    this.player.opacity = 0;
    this.player.y = -cc.winSize.height * 0.5 + 180;

    this.speedX = 8;

    cc.director.getPhysicsManager().enabled = true;
    cc.director.getPhysicsManager().enabledAccumulator = true;
    (cc.director.getPhysicsManager() as any).FIXED_TIME_STEP = 1 / 30;


    cc.director.getCollisionManager().enabled = true;

    this.xinxinbg01 = this.node.getChildByName("xinxinbg01");
    this.xinxinbg02 = this.node.getChildByName("xinxinbg02");
    this.xinxinbg03 = this.node.getChildByName("xinxinbg03");

    this.score = this.node.getChildByName("score");

    this.Ground = this.node.getChildByName("Ground");
    this.Ground.y = -cc.winSize.height * 0.5;


    const canvas = cc.find('Canvas');
    canvas.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
    canvas.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
    canvas.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);

    this.texture = new cc.Texture2D();
    wxUtils.postMessage({
      message: 'showGame'
    })
    const size = cc.view.getVisibleSize();
    wxUtils.getSharedCanvas().width = size.width;
    wxUtils.getSharedCanvas().height = size.height;
    this.eatIndex = 0;
  }

  update(dt) {
    let speed = this.skySpeed;
    this.xinxinbg01.y = this.xinxinbg01.y - speed;
    if (this.xinxinbg01.y <= -1251) {
      this.xinxinbg01.y = 3127;
    }
    this.xinxinbg02.y = this.xinxinbg02.y - speed;
    if (this.xinxinbg02.y <= -1251) {
      this.xinxinbg02.y = 3127;
    }
    this.xinxinbg03.y = this.xinxinbg03.y - speed;
    if (this.xinxinbg03.y <= -1251) {
      this.xinxinbg03.y = 3127;
    }
    this.updateSubDomain();
  }

  startGame() {
    this.player.opacity = 255;
  }

  jump() {
    this.player.getComponent("Player").begin_jump();
  }

  setScore(num) {
    let str = this.score.getComponent(cc.Label);
    let curnum = parseInt(str.string);
    str.string = curnum + num;
    (window as any).Game.GameOverScore = parseInt(str.string);
    this.eatIndex++;
    wxUtils.postMessage({
      type: 'score',
      data: {
        score: (window as any).Game.GameOverScore
      }
    })
  }

  onTouchStart(event) {
    this.player.getComponent("Player").touch_begin(event);
  }

  onTouchMove(event) {
    this.player.getComponent("Player").touch_move(event);
  }

  onTouchEnd(event) {
    this.state = 1;
    this.player.getComponent("Player").touch_end(event);
  }

  private isUpdate = false;

  updateSubDomain() {
    if (!this.texture) {
      return;
    }
    this.texture.initWithElement(wxUtils.getSharedCanvas());
    this.texture.handleLoadedTexture();
    this.subDomain.spriteFrame = new cc.SpriteFrame(this.texture)
  }
}